/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia.JPAControllers;

import Logica.Core.Personal.PlantillaHorarios;
import Persistencia.JPAControllers.exceptions.NonexistentEntityException;
import Persistencia.JPAControllers.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;

/**
 *
 * @author Luciano
 */
public class PlantillaHorariosJpaController implements Serializable {

    public PlantillaHorariosJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public PlantillaHorariosJpaController() {
        emf = Persistence.createEntityManagerFactory("SecaderoPU");

    }

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(PlantillaHorarios plantillaHorarios) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(plantillaHorarios);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findPlantillaHorarios(plantillaHorarios.getId()) != null) {
                throw new PreexistingEntityException("PlantillaHorarios " + plantillaHorarios + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(PlantillaHorarios plantillaHorarios) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            plantillaHorarios = em.merge(plantillaHorarios);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = plantillaHorarios.getId();
                if (findPlantillaHorarios(id) == null) {
                    throw new NonexistentEntityException("The plantillaHorarios with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            PlantillaHorarios plantillaHorarios;
            try {
                plantillaHorarios = em.getReference(PlantillaHorarios.class, id);
                plantillaHorarios.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The plantillaHorarios with id " + id + " no longer exists.", enfe);
            }
            em.remove(plantillaHorarios);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<PlantillaHorarios> findPlantillaHorariosEntities() {
        return findPlantillaHorariosEntities(true, -1, -1);
    }

    public List<PlantillaHorarios> findPlantillaHorariosEntities(int maxResults, int firstResult) {
        return findPlantillaHorariosEntities(false, maxResults, firstResult);
    }

    private List<PlantillaHorarios> findPlantillaHorariosEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from PlantillaHorarios as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public PlantillaHorarios findPlantillaHorarios(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(PlantillaHorarios.class, id);
        } finally {
            em.close();
        }
    }

    public int getPlantillaHorariosCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from PlantillaHorarios as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
